Private Sub cmd_Pay_installments_Click()
On Error GoTo err_cmd_Pay_installments_Click

' ..........................    

    Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("Select * From tbl_Loans Where [Payment_Month]=CDATE('" & Me.TxtMonth & "')")
    rst.MoveLast: rst.MoveFirst
    Rc = rst.RecordCount
    a1 = 0  'just a flag
    a2 = 0  'just a flag
    If Rc = 0 Then
        MsgBox "     " & Format(Me.TxtMonth, "mmmm") & " " & Year(Me.TxtMonth), vbInformation
        Exit Sub
    End If
    
    If Len(rst!Payment_Made & "") = 0 And Not IsNull(rst!Loan_Made) Then
        Select Case MsgBox("       " & Me.TxtMonth, vbYesNo + vbQuestion + vbDefaultButton1)
            Case vbYes
                For i = 1 To Rc
                    rst.Edit
                        If rst!Nr >= 6 Then
                            rst!Payment_Made = 0#
                        Else
                            If rst!Loan_Type = "Cridi" Then
                                rst!Payment_Made = rst!Loan_Made
                                rst!sadad = rst!Loan_Made
                                rst!Loan_Remise = 0
                            End If
                            If rst!Loan_Type = "Elec" Then
                                rst!Payment_Made = rst!Loan_Made
                                rst!sadad = rst!Loan_Made
                                rst!Loan_Remise = 0
                                
                            End If
                        End If
                        rst!wada3 = IIf(rst!sadad.Value > 0, "   ", "  ")
                        TheSum = TheSum + Nz(rst!Payment_Made, 0)
                    rst.Update
                    rst.MoveNext
                Next i
                    TheSum = Format(TheSum, "#,##0.00")
                    MsgBox "            " & "  " & vbLf & vbLf & "    =  " & TheSum, , " " & FrenchMonth(Month(DATE)) & Year(DATE)

' ..........................Ccp       

                '        
                If Month(Now()) = 3 Or Month(Now()) = 7 Then
                    Dim db As DAO.Database
                    Dim rstE As DAO.Recordset
                    Dim myCriteria As String
                    Dim PaymentCheck As Variant
                    Dim SelectedYear As Integer
                    
                    Set db = CurrentDb
                    SelectedYear = Year(Now())
                    myCriteria = "[detach] IN ('', '   ', '   ', '   ', '  ')"
                    Set rstE = db.OpenRecordset("SELECT * FROM Employee WHERE " & myCriteria, dbOpenDynaset)
                    
                    If Not rstE.EOF Then rstE.MoveFirst
                    
                    Do Until rstE.EOF
                        If Month(Now()) = 3 Then
                            PaymentCheck = Nz(DSum("Payment_Made", "tbl_Loans", "EmployeeID=" & rstE!EmployeeID & _
                                " AND [Payment_Month] BETWEEN #" & DateSerial(SelectedYear, 1, 1) & "# AND #" & DateSerial(SelectedYear, 2, 28) & "# AND [wada3]=' '"), 0)
                        Else
                            PaymentCheck = Nz(DSum("Payment_Made", "tbl_Loans", "EmployeeID=" & rstE!EmployeeID & _
                                " AND [Payment_Month] BETWEEN #" & DateSerial(SelectedYear, 4, 1) & "# AND #" & DateSerial(SelectedYear, 6, 30) & "# AND [wada3]=' '"), 0)
                        End If

                        If PaymentCheck >= 3000 Then GoTo NextEmployee
                        
                        Set rst = db.OpenRecordset("tbl_Loans", dbOpenDynaset)
                        rst.FindFirst "[Loan_Type]='Inkhirat' AND [EmployeeID]=" & rstE!EmployeeID & " AND [Payment_Month]=#" & Format(CDate(Me.TxtMonth), "mm/dd/yyyy") & "#"
                        
                        If rst.NoMatch Then
                            rst.AddNew
                            rst!EmployeeID = rstE!EmployeeID
                            rst!Loan_ID = 0
                            rst!Payment_Month = DateSerial(Year(Me.TxtMonth), Month(Me.TxtMonth), 1)
                            rst!Payment_Made = DLookup("Other_Value", "TblOther", "ID=1")
                            rst!Loan_Type = "Inkhirat"
                            rst!Nr = GetNumDetach(rst!EmployeeID)
                            rst!Remarks = "     " & Year(Me.TxtMonth) & "/" & Month(Me.TxtMonth)
                            rst!annee = SelectedYear
                            rst!sadad = rst!Payment_Made
                            rst!wada3 = IIf(rst!sadad > 0, "  ", " ")
                            
                            TheSum = TheSum + Nz(rst!Payment_Made, 0)
                            rst.Update

                        End If
                        rst.Close
NextEmployee:
                        rstE.MoveNext
                    Loop
                    
                    rstE.Close: Set rstE = Nothing
                    db.Close: Set db = Nothing
                   
' ..........................   
                    TheSum = Format(TheSum, "#,##0.00")
                    MsgBox "            " & "  " & vbLf & vbLf & "   =  " & TheSum, , " " & FrenchMonth(Month(DATE)) & Year(DATE)
' ..........................      
                    Call cmdversment_Click
                End If
               
I_am_Done:
            Case vbNo
                 MsgBox "   "

        End Select
        rst.Close: Set rst = Nothing
    End If
    Exit Sub
 
err_cmd_Pay_installments_Click:
    If Err.Number = 3021 Then
        Resume Next
    Else
       ' MsgBox Err.Number & vbCrLf & Err.Description
    End If
End Sub
